package com.alibaba.citrus.cr.price.ability.functions.factory.price.process;

import com.alibaba.acm.shaded.com.alibaba.metrics.StringUtils;
import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.epoch.app.crplatformenhance.domain.scitemgroup.dto.QueryScItemGroupByCodesRequest;
import com.epoch.app.crplatformenhance.domain.scitemgroup.dto.QueryScItemGroupByCodesResponse;
import com.epoch.app.crplatformenhance.domain.scitemgroup.model.ScItemGroup;
import com.epoch.app.crplatformenhance.domain.scitemgroup.service.ScItemGroupService;
import com.epoch.app.crpricecenter.enums.DimensionPropertyEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hanguo
 * @date 2021/11/19
 * @Description 产品组
 */
@Component
public class ScItemGroupProcess extends PriceProcess {
    /**
     *产品组查询服务
     */
    @Resource
    private ScItemGroupService scItemGroupService;

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainCodeAndDescMap(List<String> codeList, boolean innerCode) {
        List<ScItemGroup> scItemGroups = obtainScItemGroupList(codeList, innerCode);
        if (CollectionUtils.isEmpty(scItemGroups)) {
            return new HashMap<>();
        }

        if (innerCode) {
            return scItemGroups.stream().filter(item -> StringUtils.
                    isNotBlank(item.getCode()) && StringUtils.
                    isNotBlank(item.getName())).collect(Collectors.
                    toMap(ScItemGroup::getCode, ScItemGroup::getName, (x, y) -> x));
        }
        return scItemGroups.stream().filter(item -> StringUtils.
                isNotBlank(item.getOutCode()) && StringUtils.
                isNotBlank(item.getName())).collect(Collectors.
                toMap(ScItemGroup::getOutCode, ScItemGroup::getName, (x, y) -> x));
    }

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainInnerCodeAndOuterCodeMap(List<String> codeList, boolean innerCode) {
        List<ScItemGroup> scItemGroups = obtainScItemGroupList(codeList, innerCode);
        if (CollectionUtils.isEmpty(scItemGroups)) {
            return new HashMap<>();
        }

        if (innerCode) {
            return scItemGroups.stream().filter(item -> StringUtils.
                    isNotBlank(item.getCode()) && StringUtils.
                    isNotBlank(item.getOutCode())).collect(Collectors.
                    toMap(ScItemGroup::getCode, ScItemGroup::getOutCode, (x, y) -> x));
        }
        return scItemGroups.stream().filter(item -> StringUtils.
                isNotBlank(item.getOutCode()) && StringUtils.
                isNotBlank(item.getCode())).collect(Collectors.
                toMap(ScItemGroup::getOutCode, ScItemGroup::getCode, (x, y) -> x));
    }

    @RepositoryInvoker
    public List<ScItemGroup> obtainScItemGroupList(List<String> codeList, boolean innerCode) {
        QueryScItemGroupByCodesRequest queryScItemGroupByCodesRequest = new QueryScItemGroupByCodesRequest();
        queryScItemGroupByCodesRequest.setCodes(codeList);
        QueryScItemGroupByCodesResponse queryScItemGroupByCodesResponse = scItemGroupService.queryScItemGroupByCodes(queryScItemGroupByCodesRequest);

        if (CollectionUtils.isEmpty(queryScItemGroupByCodesResponse.getResult())) {
            return new ArrayList<>();
        }
        return queryScItemGroupByCodesResponse.getResult();

    }

    @Override
    public String getPropertyCode() {
        return DimensionPropertyEnum.SC_ITEM_GROUP_CODE.getKey();
    }
}
